// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function () { // 通用预处理器

	},
	
	/**
	 * 获取列表
	 * @param {string} openid 
	 * @param {number} pageIndex 
	 * @param {number} pageSize 
	 */
	async getList(openid, pageIndex = 1, pageSize = 100) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		let res = null;
		if (openid) {
			res = await dbjql.collection("wishlist").where({
				onlyid: openid
			}).skip((pageIndex - 1) * pageSize).limit(pageSize).get();
		} else {
			res = await dbjql.collection("wishlist").skip((pageIndex - 1) * pageSize).limit(pageSize).get();
		}
		return res;
	},
	
	/**
	 * 添加记录
	 * @param {Object} item
	 */
	async add(openid, item) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});

		const resRight = await dbjql.collection('wishlist').add({
			onlyid: openid,
			...item
		});
		return resRight;
	},
	
	/**
	 * 删除搜索记录
	 * @param {string} openid 
	 * @param {string} id 
	 */
	async delete(openid, id) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('wishlist').where({
			onlyid: openid,
			_id: id
		}).remove();
		return res;
	},
	
	/**
	 * 查询记录通过id
	 * @param {Object} id
	 */
	async query(id) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('wishlist').doc(id).get();
		return res;
	},
	/**
	 * 更新记录
	 */
	async update(id, item) {
		const dbjql = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const res = await dbjql.collection('wishlist').doc(id).update(item);
		return res;
	},
}
